package code.structure.manager.loader
{
	import code.tools.traceLog.TraceLog;
	
	import com.loadPackage.loadingtypes.ImageItem;
	import com.loadPackage.loadingtypes.LoadingItem;
	
	import flash.display.BitmapData;
	import flash.system.ApplicationDomain;
	import flash.utils.Dictionary;

	/**
	 *swf加载分析器 
	 * @author Yuri
	 * 
	 */
	public class SwfLoaderAnalyer implements IAnalyer
	{
		/**
		 * applicationDomain集合
		 */
		private var _appDomainDic:Dictionary;
		
		private var _classDic:Dictionary;
		
		private var _callBackDic:Dictionary;
		public function SwfLoaderAnalyer()
		{
			_appDomainDic = new Dictionary();
			_classDic = new Dictionary();
			_callBackDic = new Dictionary()
		}
		
		/**
		 * 获取数据
		 * @param key s文件
		 * @param subKey 具体类名
 		 * @param callBackFun 回调
		 * @return 
		 * 
		 */
		public function getData(key:String,subKey:String,callBackFun:Function=null):*{
			var classkey:String = key+"&"+subKey;
			var returnObject:*;
			//是否存在域
			var isExistApplicationDomain:Boolean;
			
			var index:int=-1;
			
			if(_classDic[classkey]){
				returnObject =_classDic[classkey];
			}else if(_appDomainDic[key]){
				isExistApplicationDomain = true;
				if((_appDomainDic[key] as ApplicationDomain).hasDefinition(subKey)){
					returnObject = (_appDomainDic[key] as ApplicationDomain).getDefinition(subKey);
					_classDic[classkey] = returnObject;
				}
			}
			/*if(callBackFun!=null){
				if(returnObject){
					callBackFun(returnObject,classkey);
				}else{
					if(!_callBackDic[key])_callBackDic[key] = new Dictionary(); 
					if(!_callBackDic[key][subKey]) _callBackDic[key][subKey] = new Array();
					index = _callBackDic[key][subKey].indexOf(callBackFun);
					if(index !=-1){
						_callBackDic[key][subKey].splice(index,1,callBackFun);
					}else _callBackDic[key][subKey].push(callBackFun);
				}
			}*/
			if(callBackFun!=null){//有回调
				if(isExistApplicationDomain){ //存在此文件
//					if(returnObject){
						callBackFun(returnObject,classkey);
//					}
				}else{
					if(returnObject){
						callBackFun(returnObject,classkey);
					}else{
						if(!_callBackDic[key])_callBackDic[key] = new Dictionary(); 
						if(!_callBackDic[key][subKey]) _callBackDic[key][subKey] = new Array();
						index = _callBackDic[key][subKey].indexOf(callBackFun);
						if(index==-1)_callBackDic[key][subKey].push(callBackFun);
					}
				}
			}
			return returnObject;
		}
		
		/**
		 *回调 
		 * @param key
		 * 
		 */
		private function callBack(key:String):void{
			if(_callBackDic[key]){
				for(var subKey:String in _callBackDic[key]){
					var callList:Array = _callBackDic[key][subKey] as Array;
					var count:int = callList.length;
					/*for(var i:int=0;i<count;i++){
						var func:Function = callList[i];
						getData(key,subKey,func);
					}*/
					for(var i:int=count-1;i>-1;i--){
						var func:Function = callList[i];
						if(func!=null)
							getData(key,subKey,func);
						callList.splice(i,1);
					}
				}
				delete _callBackDic[key];
			}
		}
		
		/**
		 *设置数据 
		 * @param item
		 * 
		 */
		public function setData(item:LoadingItem):void{
			var key:String = item.id?item.id:item.url.url;
			_appDomainDic[key] = (item as ImageItem).loader.contentLoaderInfo.applicationDomain;
			callBack(key);
		}
	}
}